//
//  MLSectionIndexView.m
//  MLSections
//
//  Created by ma c on 2017/8/10.
//  Copyright © 2017年 Mountec. All rights reserved.
//

#import "MLSectionIndexView.h"
#import "SectionToastView.h"

#define kIndexItemWidth 25
#define kMaxIndexCount 40
#define kViewWidth 30

@interface MLSectionIndexView ()


@property (nonatomic,strong)UIView *backgroundView;

@property (nonatomic,strong)NSArray <NSString *>*attachTitles;

@property (nonatomic,strong)NSMutableArray<UILabel *> *sectionIndexItems;

@property (nonatomic,strong)UITableView *attachTable;

@property (nonatomic,strong)UIBlurEffect *blurEffect;

@property (nonatomic,strong)UIVisualEffectView *visualEffectView;

@property (nonatomic,strong)SectionToastView *toastView;

@end

@implementation MLSectionIndexView
{
    CGFloat singleHeight;
    NSInteger currentIndex;
}

- (SectionToastView *)toastView {
    if (!_toastView) {
        _toastView = [[SectionToastView alloc] init];
    }
    return _toastView;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] init];
        _backgroundView.backgroundColor = [UIColor clearColor];
        _backgroundView.clipsToBounds = YES;
    }
    return _backgroundView;
}

- (UIVisualEffectView *)visualEffectView {
    if (!_visualEffectView) {
        self.blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
        _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:self.blurEffect];
    }
    return _visualEffectView;
}


- (id)initWithTable:(UITableView *)table {
    self = [super init];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
        self.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - kViewWidth, 9, kViewWidth, 0);
        self.attachTable = table;
        [self.attachTable.superview addSubview:self];
        self.sectionIndexItems = [[NSMutableArray alloc] init];
        [self addPanGes];
        
    }
    return self;
}

- (void)reloadIndexs:(NSArray<NSString *> *)indexTitles {
    
    self.attachTitles = indexTitles;
    singleHeight = 0;
    CGFloat viewHeight = 0;
    if (indexTitles.count > kMaxIndexCount) {
        singleHeight = self.attachTable.frame.size.height / indexTitles.count;
        viewHeight = self.attachTable.frame.size.height;
    }else {
        singleHeight = self.attachTable.frame.size.height / kMaxIndexCount;
        viewHeight = indexTitles.count * singleHeight;
    }
    
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, kViewWidth, viewHeight+20);
    self.center = CGPointMake(self.center.x, self.attachTable.frame.size.height / 2.0);

    self.backgroundView.frame = CGRectMake(0, 0, kIndexItemWidth, self.frame.size.height);
    [self addSubview:self.backgroundView];
    self.backgroundView.layer.cornerRadius = self.backgroundView.bounds.size.width/2.0;
    
    self.visualEffectView.frame = self.backgroundView.bounds;
    [self.backgroundView addSubview:self.visualEffectView];
    
    [self.sectionIndexItems enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    __block CGFloat off_y = 10;
    [indexTitles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, off_y, kIndexItemWidth, singleHeight)];
        label.text = obj;
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        [self addSubview:label];
        off_y = CGRectGetMaxY(label.frame);
    }];
    
}

- (void)addPanGes {
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGes:)];
    [self addGestureRecognizer:pan];
}

- (void)panGes:(UIGestureRecognizer *)gesture {
    CGFloat offy = [gesture locationInView:self].y;
    
    NSInteger index = offy/singleHeight - 1;
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [self.toastView dismiss];
    }
    
    if (index >= self.attachTitles.count || index < 0) {
        return;
    }
    if (currentIndex == index) {
        return;
    }
    currentIndex = index;
    [self.toastView show:self.attachTitles[index]];
    
    NSLog(@"index = %ld,title = %@",index,self.attachTitles[index]);
//    if (self.indexBlock) {
//        self.indexBlock(index, self.attachTitles[index]);
//    }
    [self.attachTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:index] atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
}

@end

